package a03_ThreadInsecurity;

/**
 * @Author quan
 * @Description 观察线程不安全的现象
 * @Date 2023/5/5 8:38
 */
public class a01_securityPheno {
    //定义自增操作的对象
    private static Counter counter = new Counter();
    //定义自增的对象
    public static void main(String[] args) throws InterruptedException {
        //定义两个线程，分别自增5w次
        Thread t1 = new Thread(()->{
            for (int i = 0; i < 50000; i++) {
                counter.increment();
            }
        });
        Thread t2 = new Thread(()->{
            for (int i = 0; i < 50000; i++) {
                counter.increment();
            }
        });
        //启动线程
        t1.start();
        t2.start();
        //等待自增完成
        t1.join();
        t2.join();
        //打印结果
        System.out.println("count="+counter.count);
    }
}
class Counter{
    public int count = 0;
    //设置自增方法
    public void increment(){
        count++;
    }
}
